/*
 * TOP SECRET Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary
 * information of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.web.controller.manage;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yunji.framework_template.biz.cache.CacheService;
import com.yunji.framework_template.orm.datasource.HandleDataSource;
import com.yunji.framework_template.web.quartz.news.NewsContentImageCrawlerTimer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * ClassName:ManageController <br/>
 * Date: 2018年12月6日 上午10:15:03 <br/>
 * 
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 * @version
 * @see
 */
@Controller
@RequestMapping("/management")
@Api(value = "管理专用接口", tags = "manageAPI", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ManageController {

    @Resource
    private CacheService                 cacheService;
    @Resource
    private NewsContentImageCrawlerTimer newsContentImageCrawlerTimer;

    @ApiOperation(value = "重新加载所有的缓存到内存及缓存中", notes = "重新加载所有的缓存到内存及缓存中，提升访问速度", httpMethod = "GET")
    @RequestMapping(value = "/reloadCache", method = { RequestMethod.GET })
    @ResponseBody
    public String reloadCache(HttpServletRequest request, HttpServletResponse response) throws IOException {
        cacheService.init();
        return "OK";
    }

    @ApiOperation(value = "为库中已经存在的但是没有图片的新闻抓取封面图片", notes = "为库中已经存在的但是没有图片的新闻抓取封面图片", httpMethod = "GET")
    @RequestMapping("crawleContentImage")
    @ResponseBody
    public String crawleContentImage() {
        // 放到后台去执行
        new Thread() {

            public void run() {
                newsContentImageCrawlerTimer.execute();
            }
        }.start();
        return "OK";
    }

    @ApiOperation(value = "将网站的Icon加入到内存中", notes = "将网站的Icon加入到内存中，直接从内存中获取内容返回，速度会更快", httpMethod = "GET")
    @RequestMapping(value = "/loadIcon", method = { RequestMethod.GET })
    @ResponseBody
    public String loadIcon(HttpServletRequest request, HttpServletResponse response) throws IOException {
        cacheService.initIcons();
        return "OK";
    }

    @ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "String", name = "defaultCountryCode", value = "默认的国家码（如CN）", required = true, defaultValue = "IN") })
    @ApiOperation(value = "设置默认的国家", notes = "设置默认的国家，后续的所有数据操作行为都是基于这个国家的数据操作", httpMethod = "GET")
    @RequestMapping(value = "/setDefaultCountry/{defaultCountryCode}", method = { RequestMethod.GET })
    @ResponseBody
    public String setDefaultCountry(@PathVariable("defaultCountryCode") String defaultCountryCode) throws IOException {
        HandleDataSource.setDefaultCountryCode(defaultCountryCode);
        return "OK";
    }
    
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "Boolean", name = "crawleSubPages", value = "设置爬虫抓取时是否抓取下一页", required = true, defaultValue = "true") })
    @ApiOperation(value = "设置爬虫抓取时是否抓取下一页", notes = "设置爬虫抓取时是否抓取下一页", httpMethod = "GET")
    @RequestMapping(value = "/setCrawleSubPages/{crawleSubPages}", method = { RequestMethod.GET })
    @ResponseBody
    public String setCrawleSubPages(@PathVariable("crawleSubPages") boolean crawleSubPages) throws IOException {
        cacheService.getCountryCache().setCrawleSubPages(crawleSubPages);
        return "OK";
    }
    
    @ApiOperation(value = "加载各个国家的新闻到内存", notes = "加载各个国家的新闻到内存", httpMethod = "GET")
    @RequestMapping(value = "/initLatestNews", method = { RequestMethod.GET })
    @ResponseBody
    public String initLatestNews(HttpServletRequest request, HttpServletResponse response) throws IOException {
        cacheService.initLatestNews();
        return "OK";
    }
    
    @ApiOperation(value = "加载各个国家的App到内存", notes = "加载各个国家的App到内存", httpMethod = "GET")
    @RequestMapping(value = "/initApps", method = { RequestMethod.GET })
    @ResponseBody
    public String initApps(HttpServletRequest request, HttpServletResponse response) throws IOException {
        cacheService.initApps();
        return "OK";
    }
}
